<template>
    <el-row class="login-container">
        <el-col :lg="16" :md="12" class="left">
            <div class="title">欢迎光临</div>
            <div class="text-gray-200 text-sm">电子商务综合服务管理系统</div>
        </el-col>
        <el-col :lg="8" :md="12" class="right">
            <h2 class="font-bold text-3xl text-gray-800">欢迎回来</h2>
            <div class="flex items-center justify-center my-5 text-gray-300 space-x-2">
                <span class="h-[1px] w-16 bg-gray-200"></span>
                <span>账号密码登录</span>
                <span class="h-[1px] w-16 bg-gray-200"></span>
            </div>

            <el-form ref="formRef" :model="form" :rules="rules" class="w-[250px]">
                <el-form-item prop="username">
                    <el-input v-model="form.username" placeholder="请输入账号">
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <user />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password" type="password">
                    <el-input v-model="form.password" placeholder="请输入密码" show-password>
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button round color="#626aef" class="w-[250px]" type="primary" @click="onSubmit"
                        :loading="loading">登
                        录</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>

<script setup>
import { ref, reactive,onMounted,onBeforeUnmount } from 'vue'
import { toast } from '~/composables/util'
import { setToken } from '~/composables/auth'
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
const router = useRouter()
const store = useStore()
const form = reactive({
    username: '',
    password: '',

})

const rules = {
    username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
        // { min: 6, max: 10, message: '密码长度必须是6-10个字符', trigger: 'blur' },
    ],
}

const formRef = ref(null)
const loading = ref(false)
const onSubmit = () => {
    formRef.value.validate((validate) => {
        if (!validate) {
            return false
        }
        loading.value = true
        store.dispatch('login',form).then(res=>{
            toast("登录成功","success")
            router.push('/')
        }).finally(() => {
            loading.value = false
        })
        
    })

}

// 监听回车事件
function onKeyUp(e){
    if(e.key ==="Enter"){
        onSubmit()
    }
    console.log(e);
}
// 添加键盘监听
onMounted(()=>{
   document.addEventListener("keyup",onKeyUp) 
})

onBeforeUnmount(()=>{
    document.removeEventListener("keyup",onKeyUp) 
})

</script>
<style scoped>
.login-container {
    @apply bg-indigo-500 min-h-screen;
}

.login-container .left,
.login-container .right {
    @apply flex flex-col items-center justify-center;
}

.login-container .right {
    @apply bg-light-50 flex-col;
}

.login-container .title {
    @apply font-bold text-4xl text-light-50 mb-4
}
</style>